package com.ophiux.cs.application.user.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ophiux.cs.application.core.Result;
import com.ophiux.cs.application.core.ResultEnum;
import com.ophiux.cs.application.user.dto.UpdateUserPatientRequest;
import com.ophiux.cs.application.user.dto.UpdateUserPatientResponse;
import com.ophiux.cs.application.user.service.IUpdateUserPatientAppService;
import com.ophiux.cs.domain.user.model.entity.Patients;
import com.ophiux.cs.infrastructure.repositories.user.PatientRepository;
import com.ophiux.utils.common.ShiftUtils;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class UpdateUserPatientAppService implements IUpdateUserPatientAppService {
	@Autowired
	PatientRepository patientRepository;
	
	@Override
	public Result<UpdateUserPatientResponse> UpdateUserPatient(UpdateUserPatientRequest dto) {
		UpdateUserPatientResponse resp = new UpdateUserPatientResponse();
		try {
			if(!dto.isParamValid()) {
				return Result.fail(ResultEnum.requestParamFail);
			}
			
			Patients patient = patientRepository.getPatientsByPatientId(dto.getPATIENT_ID());
			if(patient==null) {
				return Result.fail(ResultEnum.NotFoundPatient);
			}
			patient = (Patients) ShiftUtils.objToNewObj(dto, Patients.class);
			
			patientRepository.save(patient);

			resp.setPATIENT_ID(patient.getPATIENT_ID());
		} catch (Exception e) {
			log.info("程序异常：",e);
			e.printStackTrace();
			return Result.fail(ResultEnum.requestException);
		}
		return Result.success(null);
	}
}
